<?php
// 公共配置文件
use cnbbx\Env;
use cnbbx\Http;
use cnbbx\Response;
use cnbbx\Session;

/**
 * @return Http
 */
function Http()
{
    return config('Http');
}

/**
 * @return Response
 */
function Response()
{
    return config('Response');
}

/**
 * 获取和设置配置参数
 * @param $name
 * @param null $value
 * @return mixed
 */
function config($name, $value = null)
{
    $value && Env::set($name, $value);
    return Env::get($name);
}

/**
 * json正确返回
 * @param $data
 * @param $total
 * @param $page
 * @return array
 */
function json($data, $total = 0, $page = 0)
{
    if (is_string($data)) {
        $ret = array('res' => 1, 'msg' => $data);
    } elseif (is_array($data) && count($data) == count($data, 1)) {
        $ret = array('res' => 1, 'data' => $data);
    } else {
        $ret = array('res' => 1, 'items' => $data, 'total' => $total, 'page' => $page);
    }
    return $ret;
}

/**
 *json错误返回
 * @param $data
 * @return array
 */
function error($data)
{
    return array('res' => 0, 'msg' => $data);
}

/**
 * 浏览器友好的变量输出
 * @param $var
 */
function dump($var)
{
    isCli() ? export($var) : export("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body><style>pre { padding: 12px; overflow: auto; background: #f0f0f0;line-height: 20px; }</style><pre>" . htmlspecialchars(print_r($var, true)) . "</pre></body></html>", false);
}

/**
 * 获取输入数据 支持默认值和过滤
 * @param $name
 * @return mixed
 */
function input($name, $def = 0)
{
    $request = config('Request');
    return isset($request[$name]) ? $request[$name] : $def;
}

/**
 * Cookie管理
 * @param $name
 * @return mixed
 */
function cookie($name, $value = null)
{
    if (is_null($value)) {
        $cookie = config('Cookie');
        return $cookie[$name];
    } else {
        setcookie($name, $value, time() + 3600 * 48);
        return $value;
    }
}

/**
 * Session管理
 * @param $name
 * @return mixed
 */
function session($name, $value = null)
{
    $sess = Session::getSessionInterface();
    if (is_null($value)) {
        return $sess[$name];
    } else {
        $sess[$name] = $value;
        return $value;
    }
}

/**
 * 响应输出
 * @param $var
 * @param bool $end
 */
function export($var, $end = true)
{
    $response = Response();
    $response->setContent($var);
    $response->send();
    $end && Http()->end($response);
}

/**
 * 获取当前客户端IP地址
 * @return mixed|string|null
 */
function getIp()
{
    $server = config('Server');
    if (isset($server['REMOTE_ADDR']) && filter_var($server['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
        $ip = $server['REMOTE_ADDR'];
    } elseif (isset($server['HTTP_X_FORWARDED_FOR'])) {
        $arr = explode(',', $server['HTTP_X_FORWARDED_FOR']);
        $ip = $arr[0];
    } else {
        $ip = '127.0.0.1';
    }
    return $ip;
}

/**
 * 远程接口调用
 * @param string $url 不要超过255位
 * @param string $data
 * @param int $timeOut
 * @param array $headers
 * @return mixed
 */
function post($url = null, $data = null, $timeOut = 5, $headers = null)
{
    if ($url == null) {
        return "";
    }

    $ch = @curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeOut * 2);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
    if ($headers && is_array($headers)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_HEADER, 0);
    }
    if ($data) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
    }
    $result = @curl_exec($ch);
    if ($result == false) {
        error_log(curl_error($ch));
    }
    error_log(curl_error($ch));
    curl_close($ch);
    return $result;
}

/**
 * 是否为cli
 * @access public
 * @return bool
 */
function isCli()
{
    return PHP_SAPI == 'cli';
}

/**
 * 是否为cgi
 * @access public
 * @return bool
 */
function isCgi()
{
    return strpos(PHP_SAPI, 'cgi') === 0;
}
